//
//  HNSH_NewFriendViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/22.
//

#import "HNSH_NewFriendViewController.h"

#import "HNSH_NewFriendCell.h"

#import "HNSH_PassFriendViewController.h"

@interface HNSH_NewFriendViewController ()
/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;
@end

static NSString *HNSH_NewFriendCellID = @"HNSH_NewFriendCellID";

@implementation HNSH_NewFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"新的朋友" showLeftItem:YES];
    [self configUI];
    
    [self loadNewFriendshipList];
}

- (void)configUI {
    self.tableView.rowHeight = 55;
    [self.tableView registerClass:[HNSH_NewFriendCell class] forCellReuseIdentifier:HNSH_NewFriendCellID];
}

#pragma mark lazy
//好友请求列表
- (void)loadNewFriendshipList {
    [HNSH_ChatNetServer requestRongCloudNewFriendshipListSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSArray * list = responseObject.resultset;
            NSArray * array = [NSArray yy_modelArrayWithClass:HNSH_ChatFriendModel.class json:list];
            self.listAry = [NSMutableArray arrayWithArray:array];
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)loadFriendshipIgnore:(NSString *)targetId {
    if (!targetId) {
        return;
    }
    [HNSH_ChatNetServer requestRongCloudFriendshipIgnoreWithRcUserId:targetId success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"拒绝成功"];
            [self loadNewFriendshipList];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: 拒绝
- (void)ignoreWithCell:(HNSH_NewFriendCell *)cell {
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    HNSH_ChatFriendModel * model = self.listAry[indexPath.row];
    __weak __typeof(&*self)weakSelf = self;
    [HNSH_AlertView showTitle:@"是否拒绝对方好友申请" des:@"" buttonTitle:@"拒绝" buttonColor:[UIColor appGreenColor] buttonClick:^{
        [weakSelf loadFriendshipIgnore:model.user.user_id];
    }];
}

//MARK: 接受
- (void)acceptWithCell:(HNSH_NewFriendCell *)cell{
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    HNSH_ChatFriendModel * model = self.listAry[indexPath.row];
    HNSH_PassFriendViewController * vc = [HNSH_PassFriendViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_NewFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_NewFriendCellID];
    cell.model = self.listAry[indexPath.row];
    WeakSelf(weakSelf);
    cell.acceptBlock = ^(HNSH_NewFriendCell * _Nonnull selCell) {
        [weakSelf acceptWithCell:selCell];
    };
    cell.ignoreBlock = ^(HNSH_NewFriendCell * _Nonnull selCell) {
        [weakSelf ignoreWithCell:selCell];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

@end
